package com.dom.stor.dao.service.impl;

import com.dom.common.enums.StorMsgTypeEnum;
import com.dom.stor.dao.entity.TGoodsInfo;
import com.dom.stor.dao.entity.TMeasureUnitTbl;
import com.dom.stor.dao.entity.TStorInfo;
import com.dom.stor.dao.entity.TsStorMsgFlow;
import com.dom.stor.dao.mapper.TsStorMsgFlowMapper;
import com.dom.stor.dao.service.ITGoodsInfoService;
import com.dom.stor.dao.service.ITMeasureUnitTblService;
import com.dom.stor.dao.service.ITStorInfoService;
import com.dom.stor.dao.service.ITsStorMsgFlowService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * S_库存变动流水表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-13
 */
@Service
public class TsStorMsgFlowServiceImpl extends ServiceImpl<TsStorMsgFlowMapper, TsStorMsgFlow> implements ITsStorMsgFlowService {
    @Autowired
    private ITGoodsInfoService goodsInfoService;
    @Autowired
    private ITStorInfoService storInfoService;
    @Autowired
    private ITMeasureUnitTblService measureUnitTblService;

    @Override
    public boolean buildStorageMsg(int memCode, Integer storCode, Integer goodCode, int msgType, BigDecimal amount) {
        TsStorMsgFlow storMsgFlow = new TsStorMsgFlow();

        int msgWay = 0;
        if(StorMsgTypeEnum.MSG_TYPE_STOCKIN.getCode() == msgType){
            /**
             * 入库单
             */
            msgWay = 1;

        } else if (StorMsgTypeEnum.MSG_TYPE_STOCKOUT.getCode() == msgType) {
            /**
             * 出库单扣减库存
             */
            msgWay = -1;
        }

        TStorInfo storInfo = storInfoService.getStorInfo(memCode, storCode);
        TGoodsInfo goodInfo = goodsInfoService.getGoodInfo(memCode, goodCode);
        TMeasureUnitTbl unitTbl = measureUnitTblService.getUnitInfo(memCode, goodInfo.getUnitCode());

        storMsgFlow.setMemCode(memCode);
        storMsgFlow.setMsgType(msgType);
        storMsgFlow.setMsgWay(msgWay);
        storMsgFlow.setStorCode(storCode);
        storMsgFlow.setStorAbbr(storInfo.getStorAbbr());
        storMsgFlow.setGoodCode(goodCode);
        storMsgFlow.setGoodName(goodInfo.getGoodName());
        storMsgFlow.setVarietyCode(goodInfo.getVarietyCode());
        storMsgFlow.setStandardName(goodInfo.getGoodName());
        storMsgFlow.setUnit(unitTbl.getUnit());
        storMsgFlow.setAmount(amount);
        return this.insert(storMsgFlow);
    }
}
